/**
 * Created by lghdb on 2018/7/9.
 */
var api = require('../lib/docker_api');
var user = require('../lib/user');

var index = async (ctx, next) => {
    var reqs = await api.images();
    ctx.render("index.html", {
        "title":"Docker首页",
        "reqs": reqs
    });
};

var image = async(ctx, next) => {
    var image = ctx.params.image;
    var tag = ctx.params.tag;
    var value = await api.imageInfo(image, tag);
    delete value.history;
    ctx.render("image_detail.html", {
        "title":`Docker镜像[${image}]信息`,
        "image": JSON.stringify(value)
    });
};

var login = async(ctx, next) => {
    if (ctx.request.method == 'GET'){
        ctx.render("login.html", {});
    }else if (ctx.request.method == 'POST'){
        console.log(ctx.request.body);
        var username = ctx.request.body.username;
        var passwd = ctx.request.body.passwd;
        console.log(`username = ${username} passwd = ${passwd}`);
        if (user.login(username, passwd)){
            ctx.session.loginUser = username;
            ctx.response.redirect('/');
        }else{
            ctx.render("login.html", {});
        }
    }
}

module.exports = {
    "GET /" : index,
    "GET /:image/:tag": image,
    "GET /login": login,
    "POST /login": login
}